package com.xiaocz.hblsubstitutedriving.activities.activity.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.xiaocz.common.Common;
import com.xiaocz.common.http.AsyncHttpPostFormData;
import com.xiaocz.common.http.AsyncHttpResponse;
import com.xiaocz.common.http.AsyncOkHttpClient;
import com.xiaocz.common.http.AsyncResponseHandler;
import com.xiaocz.common.model.RspModel;
import com.xiaocz.common.utils.AccountManagers;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.activities.activity.VipActivity;
import com.xiaocz.hblsubstitutedriving.base.NativeActivity;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.OnClick;

/**
 * Time:2019/11/7
 * Created by xiaocz
 * Describe vip充值方式
 */
public class VipSelectActivity extends NativeActivity {

    public static void show(Context context) {
        Intent intent = new Intent(context, VipSelectActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.dialog_vip_select;
    }


    @OnClick({R.id.tv_pay, R.id.tv_yj})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_pay:
                VipActivity.show(mContext);
                finish();
                break;
            case R.id.tv_yj:
                dialogHint();
                break;
        }
    }

    /**
     * 提示
     */
    private void dialogHint() {
        final Dialog alert = new Dialog(mContext);
        alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alert.setContentView(R.layout.dialog_base);
        TextView tvMessage = alert.findViewById(R.id.tv_message);
        tvMessage.setText("确认选用方式二：信用预借？");
        alert.show();
        WindowManager.LayoutParams lp = alert.getWindow().getAttributes();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        lp.width = (int) (dm.widthPixels * 0.92); //设置宽度
        alert.getWindow().setAttributes(lp);
    }


    /**
     * 设置vip充值类型
     */
    private void webPayType() {
        loadingDialogShow(false);
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("driver_id", AccountManagers.getUserId());
        asyncHttpPostFormData.addFormData("driverRechargeConfig_id", "0");
        asyncHttpPostFormData.addFormData("rechargeType", "2");//1-直接支付 2-平台预借 3-预借完成
        okHttpClient.post(Common.Constant.API_IP + "api/Driver/createDriverRechargeOrder", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                onDismiss();
                App.getInstance().showMessage(getResources().getString(R.string.connection_error));
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                onDismiss();
                Type type = new TypeToken<RspModel<String>>() {
                }.getType();
                RspModel<String> rspModel = getAnalysis(response, type, "设置vip充值类型");
                if (rspModel == null) {
                    return;
                }
                switch (rspModel.getCode()) {
                    case 200:
                        if (rspModel.isStatus()) {
                            finish();
                        }
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                    default:
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }
}
